#!/usr/bin/env bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/functions

MONGO_VERSION=${MONGODB_VERSION:-"3.6"}

desc 'RUN CONSISTENCY TESTS'

desc 'Start cluster'
start_cluster "$MONGO_VERSION"

desc 'Run tests'
docker-compose -f $COMPOSE_PATH start tests
docker-compose -f $COMPOSE_PATH logs -f --no-color tests
EXIT_CODE=$(docker-compose -f $COMPOSE_PATH ps -q tests | xargs docker inspect -f '{{ .State.ExitCode }}')

if [ $EXIT_CODE != 0 ]; then
    docker-compose -f $COMPOSE_PATH logs --no-color --tail=100
fi

desc 'Destroy cluster'
destroy_cluster

exit $EXIT_CODE
